<?php

namespace View;

class TopicView
{
	//Strängplatser för $_POST	
	private $getHeader = "Header";
	private $getContent = "Content";
	private $getCreateKey = "CreateKey";	
	private $getEditTopic = "EditKey";
	private $getConfirmEditTopic = "ConfirmEditKey";
	private $getDeleteTopic = "DeleteKey";	
	
	//Konstanter
	const NEW_TOPIC_HEADER = 1;
	const MY_TOPICS_HEADER = 2;
	const WAS_LOGGED_OUT = 3;
	const NO_TOPICS = 4;
	const MOST_RECENT_TOPICS = 5;
	const MOST_POPULAR_TOPICS = 6;
	const TOPIC_DOESNT_EXIST = 7;	
	const TOPIC_SUCCSESSFULLY_DELETED = 8;
	
	//Retunerar rubriker via konstantanrop
	public function RenderHeader($header)
	{
		switch($header)
		{
			case self::NEW_TOPIC_HEADER;
				return "<div class='contentHeader'><h2>New topic</h2></div>";
				break;	
			case self::MY_TOPICS_HEADER;
				return "<div class='contentHeader'><h2>My topics</h2></div>";
				break;	
			case self::MOST_RECENT_TOPICS;
				return "<div class='contentHeader'><h2>Most recent topics</h2></div>";
				break;	
			case self::MOST_POPULAR_TOPICS;
				return "<div class='contentHeader'><h2>Most popular topics</h2></div>";
				break;																					
		}			
	}	

	//Retunerar meddelanden via konstantanrop	
	public function RenderMessage($message)
	{
		switch($message)
		{
			case self::WAS_LOGGED_OUT;
				return "<p class='msg'>You are no longer logged in. Please login to continue.</p>";
				break;
			case self::NO_TOPICS;
				return "<p class='msg'>You have not created any new topics</p>";
				break;
			case self::TOPIC_DOESNT_EXIST;
				return "<p class='msg'>The given topic id doesnt exist</p>";
				break;	
			case self::TOPIC_SUCCSESSFULLY_DELETED;
				return "<p class='msg'>The Topic was successfully deleted</p>";
				break;						
		}
		
	}

	//Retunerar felmeddelanden via array-anrop
	public function RenderErrorArray($errorArray)
	{
		$ret = "<div class='errorMsg'>";
		
		foreach($errorArray as $error)
		{
			$ret .= "<p>" . $error . "</p>";
		}
			
		$ret .= "</div>";					
		return $ret;
	}	

	//Retunerar lista av Topic-objekt via medskickad array.			
	public function RenderTopicList(\Model\TopicArray $topicArray)
	{
		$ret = NULL;
		
		foreach ($topicArray->get() as $topic)
		{
			$topicLink = NavigationView::getTopicLink($topic);
			$ret .= "<div class='topics'><a href='$topicLink' class='listHeader'>" . $topic->getHeader() . "</a>
					 <p class='nameAndDate'>By " . $topic->getUserName() . " " . $topic->getDate() . "</p>
					 <p class='postCount'>" . $topic->getPostCount() . "</div>";
		}
		
		return $ret;
	}
	
	//Retunerar html för presentation av ett Topic via medskickat Topic-objekt.
	//Retunerar knappar för redigering och borttagning, om objektets anv-id överensstämmer med den inloggades anv-id	
	public function RenderTopic(\Model\Topic $topic, $loggedInUserId)
	{
		$ret =  "<div class='thread'><div class='topicHeader'>";
		$ret .= "<h2>" . $topic->getHeader() . "</h2>";
		$ret .= "<p class='nameAndDate'>By " . $topic->getUserName() . " " . $topic->getDate() . "</p></div>";
		$ret .= "<p class='topicContent'>" . $topic->getContent() . "</p></div>";
		
		if($topic->getUserId() == $loggedInUserId)
		{
			$ret .= "<form method='post' class='editDeleteTopic'>";
			$ret .= "<input type='submit' value='Delete topic' name='$this->getDeleteTopic' class='button' id='deleteTopic' />";
			$ret .= "<input type='submit' value='Edit topic' name='$this->getEditTopic' class='button' /></form>";			
		}
		
		return $ret;
	}
	
	//Retunerar formulär för skapande av nytt Topic
	public function RenderNewTopicForm()
	{
	    return $newTopicForm = 

	    "<form name='frmNewTopic' class='topicForm' method='post'>	    		
			<p>Header:</p>			
			<input type='text' maxlength='50' name='$this->getHeader' class='formHeader'/>
			<p>Content:</p>							
			<textarea type='text' name='$this->getContent' class='contentBox'></textarea> 			
			<input type='submit' value='Create' name='$this->getCreateKey' class='button' />				  			
		</form>";		
	}

	//Retunerar editeringsformulär tillhörande medskickat topic-objekt
	public function RenderEditTopicForm(\Model\Topic $topic)
	{
		//Hämtar URL som leder tillbaka till objektets visnings-sida
		$cancelLink = NavigationView::getTopicLink($topic);
	    
	    return $newEditForm = 

	    "<form name='frmEditTopic' class='topicForm' method='post'>	    		
			<p>Header:</p>			
			<input type='text' value='" .$topic->getHeader() . "' name='$this->getHeader' maxlength='50' class='formHeader'/>
			<p>Content:</p>							
			<textarea type='text' value='' name='$this->getContent' class='contentBox'>" . $topic->getContent() . "</textarea> 			
			<a href='" .$cancelLink. "'><input type='button' name='cancel' value='Cancel' class='button' /></a>	
			<input type='submit' value='Update' name='$this->getConfirmEditTopic' class='button' />			  			
		</form>";		
	}

	//Retunerar länk sidan för skapande av nytt topic	
	public function RenderNewTopicLink()
	{
		$newTopicLink = NavigationView::getNewTopicLink();
		return "<a id='newTopic' href='$newTopicLink'>Create new topic</a>";		
	}

	//Retunerar länk sidan "My topics and posts"	
	public function RenderMyTopicsAndPostsLink()
	{
		$myTopicsAndPostsLink = NavigationView::getMyTopicsAndPostsLink();
		return "<a id='TopicAndPosts' href='$myTopicsAndPostsLink'>My topics and posts</a>";		
	}	
	
	//Retunerar topic-rubriken, annars NULL
	public function GetHeader() 
	{	  	
		if (isset($_POST[$this->getHeader]) == TRUE)
		{				 		
			return $_POST[$this->getHeader];
		}
		
		return NULL;		
	}
	
	//Retunerar topic-innehåll, annars NULL
	public function GetContent() 
	{	
		if (isset($_POST[$this->getContent]) == TRUE)
		{
			return $_POST[$this->getContent];
		}
				
		return NULL;			
	}

	//Retunerar TRUE om anv trycker på "Create", annars FALSE
	public function TriedToCreateTopic() 
	{	
		if (isset($_POST[$this->getCreateKey]) == TRUE)
		{
			return TRUE;
		}
				
		return FALSE;			
	}	

	//Retunerar TRUE om anv trycker på "Edit topic", annars FALSE	
	public function TriedToEditTopic() 
	{	
		if (isset($_POST[$this->getEditTopic]) == TRUE)
		{
			return TRUE;
		}
				
		return FALSE;			
	}

	//Retunerar TRUE om anv trycker på "Update", annars FALSE		
	public function TriedToConfirmEdit() 
	{	
		if (isset($_POST[$this->getConfirmEditTopic]) == TRUE)
		{
			return TRUE;
		}
				
		return FALSE;			
	}	

	//Retunerar TRUE om anv trycker på "Delete", annars FALSE		
	public function TriedToDeleteTopic() 
	{	
		if (isset($_POST[$this->getDeleteTopic]) == TRUE)
		{
			return TRUE;
		}
				
		return FALSE;			
	}		
		
}


?>